import {observable, action, toJS} from 'mobx';
import {WeaTableNew, WeaForm} from 'comsMobx';
import {WeaLocaleProvider} from "ecCom";
import {message, Modal} from "antd";
import * as contractManagementApi from '../api/ContractManagementApi';


const confirm = Modal.confirm
const {TableStore} = WeaTableNew;
const {getLabel} = WeaLocaleProvider;

/**
 * 模板类型store
 */
class MouldTypeStore {

    /**
     * 是否有操作页面的权限
     *
     * @type {boolean}
     */
    @observable authorized = true;

    /**
     * 加载状态
     */
    @observable loading = false;

    /**
     * 分页控件的store
     */
    @observable tableStore = new TableStore();

    /**
     * 新建编辑表单
     *
     * @type {WeaForm}
     */
    @observable form = new WeaForm();

    /**
     * 分类列表选择值
     *
     * @type {string}
     */
    @observable selectedRowKeys = "";

    /**
     * 删除按钮置灰
     *
     * @type {boolean}
     */
    @observable deleteBtnDisbled = true;

    /**
     * 弹出框是否显示
     *
     * @type {boolean}
     */
    @observable dialogVisible = false;

    /**
     * 是否是编辑
     *
     * @type {boolean}
     */
    @observable edit = false;

    /**
     * 编辑的模板类型的主键
     *
     * @type {string}
     */
    @observable editRowKey = "";


    /**
     * 获取列表数据
     * @param params
     */
    @action
    getTableData = (params) => {
        this.loading = true;
        contractManagementApi.getMouldTypeList(params).then(res => {
            this.loading = false;
            const {code, data} = res;
            if (code == 401) {
                this.authorized = false;
            } else if (code == 200) {
                const {sessionkey} = data;
                this.tableStore.getDatas(sessionkey);
            }
        });
    }

    /**
     * 打开新建编辑模板类型弹框
     *
     * @param id
     */
    @action
    openDialog = (id) => {
        const method = id ? 'edit' : 'add';
        if (id) {
            this.edit = true;
            this.editRowKey = id;
            this.getEditForm({id, method});
        } else {
            this.edit = false;
            this.getEditForm({id});
        }
        this.dialogVisible = true;
    }

    /**
     * 关闭新建编辑弹框
     */
    @action
    closeDialog = () => {
        this.dialogVisible = false;
    }

    /**
     * 获取编辑表单
     *
     * @param params
     */
    @action
    getEditForm = (params) => {
        contractManagementApi.getMouldTypeEditForm(params).then(res => {
            const {code, data} = res;
            if (code == 200 && data) {
                this.form = new WeaForm();
                this.form.setCondition(data);
            }
        });
    }

    /**
     * 删除模板类型
     *
     * @param params
     */
    @action
    deleteMouldType = (params) => {
        contractManagementApi.deleteMouldType(params).then(res => {
            const {code} = res;
            if (code == 200) {
                message.success(getLabel("518560", "操作成功"));
                this.getTableData();
                this.deleteBtnDisbled = true;
                this.selectedRowKeys = [];
            } else {
                message.error(getLabel("518561", "操作失败"));
            }
        });
    }

    /**
     * 分页列表操作动作
     *
     * @param record
     * @param index
     * @param operate
     * @param flag
     */
    @action
    onOperatesClick = (record, index, operate, flag) => {
        flag == "0" && this.openDialog(record.id);
        flag == "1" && this.affirmDelete(record.id);
    }

    /**
     * 删除选中的模板类型
     *
     * @param ids
     * @param multiDelete
     */
    @action
    affirmDelete = (ids, multiDelete) => {
        if (multiDelete) {
            if (this.selectedRowKeys.length <= 0) {
                message.error(getLabel("518571", "请先选择待删除数据"));
                return;
            }
            ids = this.selectedRowKeys.join(",");
        }
        confirm({
            title: getLabel('518566', "信息确认"),
            content: getLabel('518567', "确定要删除吗？"),
            okText: getLabel('518568', "是"),
            cancelText: getLabel('518569', "否"),
            onOk: () => this.deleteMouldType({ids}),
            onCancel() {
            },
        });
    }

    /**
     * 列表check框选择事件
     *
     * @param selectedKeys
     * @param selectedInfo
     */
    @action
    rowSelection = (selectedKeys, selectedInfo) => {
        this.selectedRowKeys = selectedKeys;
        this.deleteBtnDisbled = selectedKeys.length > 0 ? false : true;
    }

    /**
     * 保存表单
     */
    @action
    saveForm = () => {
        this.form.validateForm().then(f => {
            if (f.isValid) {
                this.onSave();
            } else {
                f.showErrors();
            }
        });
    }

    /**
     * 执行模板类型数据保存
     */
    @action
    onSave = () => {
        this.loading = true;
        const formDatas = this.form.getFormParams();
        const id = this.editRowKey;
        const params = {
            ...formDatas, id
        }
        const saveFunc = this.edit ? contractManagementApi.editMouldType : contractManagementApi.addMouldType;
        saveFunc(params).then(res => {
            this.loading = false;
            const {code, data} = res;
            if (code == 200) {
                this.selectedRowKeys = [];
                this.deleteBtnDisbled = true;
                this.closeDialog();
                this.getTableData();
                message.success(getLabel("518560", "操作成功"));
            } else {
                message.error(getLabel("518561", "操作失败"));
            }
        });
    }


}

export default MouldTypeStore;